package com.ygqh.baby.service.impl;

import java.util.List;

import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.YgWholesalerRechargeRecordMapper;
import com.ygqh.baby.po.YgWholesaler;
import com.ygqh.baby.po.YgWholesalerRechargeRecord;
import com.ygqh.baby.service.YgWholesalerRechargeRecordService;
import com.ygqh.baby.service.YgWholesalerService;
import org.springframework.util.Assert;

import java.util.Arrays;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.ygqh.baby.po.YgWholesalerRechargeRecordExample;
import com.ygqh.baby.ao.DataStatus;

/**
 * @author Ss
 */
@Service
public class YgWholesalerRechargeRecordServiceImpl implements YgWholesalerRechargeRecordService {

	@Autowired
	private YgWholesalerRechargeRecordMapper ygWholesalerRechargeRecordMapper;
	@Autowired
	private YgWholesalerService ygWholesalerService;

	@Override
	public List<YgWholesalerRechargeRecord> find() {
		YgWholesalerRechargeRecordExample example = new YgWholesalerRechargeRecordExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygWholesalerRechargeRecordMapper.selectByExample(example);
	}

	@Override
	public int findCount() {
		YgWholesalerRechargeRecordExample example = new YgWholesalerRechargeRecordExample();
		example.createCriteria().andStatusEqualTo(DataStatus.Valid.name());
		return ygWholesalerRechargeRecordMapper.countByExample(example);
	}

	@Override
	public YgWholesalerRechargeRecord findById(Long id) {
		return ygWholesalerRechargeRecordMapper.selectByPrimaryKey(id);
	}


	@Override
	public int save(YgWholesalerRechargeRecord ygWholesalerRechargeRecord) {
		return ygWholesalerRechargeRecordMapper.insertSelective(ygWholesalerRechargeRecord);
	}

	@Override
	public int update(YgWholesalerRechargeRecord ygWholesalerRechargeRecord) {
		return ygWholesalerRechargeRecordMapper.updateByPrimaryKeySelective(ygWholesalerRechargeRecord);
	}

	@Override
	public int deleteSoft(Long[] ids) {
		Assert.notEmpty(ids);
		YgWholesalerRechargeRecordExample example = new YgWholesalerRechargeRecordExample();
		example.createCriteria().andIdIn(Arrays.asList(ids));
		YgWholesalerRechargeRecord record = new YgWholesalerRechargeRecord();
		record.setStatus(DataStatus.Delete);

		return ygWholesalerRechargeRecordMapper.updateByExampleSelective(record, example);
	}

	@Override
	public int deleteSoft(Long id) {
		return this.deleteSoft(new Long[]{id});
	}

	@Override
	public int saveOrUpdate(YgWholesalerRechargeRecord ygWholesalerRechargeRecord) {
		int r;
		if (ygWholesalerRechargeRecord.getId() != null) {
			r = this.update(ygWholesalerRechargeRecord);
		} else {
			r = this.save(ygWholesalerRechargeRecord);
		}
		return r;
	}

	@Override
	public ResultSet<YgWholesalerRechargeRecord> search(QueryInfo queryInfo, Long wholesalerId) {
		int count = ygWholesalerRechargeRecordMapper.countSuper(wholesalerId);
		if (count == 0) {
			return new ResultSet<>();
		}
		List<YgWholesalerRechargeRecord> list = ygWholesalerRechargeRecordMapper.selectSuper(queryInfo, wholesalerId);
		return new ResultSet<>(count, list);
	}

	@Override
	public void saveRechargeRecord(YgWholesalerRechargeRecord rechargeRecord) {
		this.validateParamForRecharge(rechargeRecord);
		this.save(rechargeRecord);

		YgWholesaler wholesaler = new YgWholesaler();
		wholesaler.setId(rechargeRecord.getWholesalerId());
		wholesaler.setTotalPrepaidPrice(rechargeRecord.getRechargePrice());
		wholesaler.setUpdateBy(rechargeRecord.getCreateBy());
		wholesaler.setUpdateTime(rechargeRecord.getCreateTime());
		ygWholesalerService.updateRechargePrice(wholesaler);
	}

	private void validateParamForRecharge(YgWholesalerRechargeRecord rechargeRecord) {
		Assert.notNull(rechargeRecord.getWholesalerId(), "批发商id不能为空");
		Assert.notNull(rechargeRecord.getRechargePrice(), "充值金额不能为空");
		YgWholesaler wholesaler = ygWholesalerService.findById(rechargeRecord.getWholesalerId());
		Assert.notNull(wholesaler, "批发商不存在");
	}

}

